package comzztpc.mzb.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;

import comzztpc.mzb.myapplication.entity.Kaifa;
import comzztpc.mzb.myapplication.entity.Xiao;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    /**
     * 首先配置环境
     * 创建Retrofit拼接网址
     * 接口GET网络地址并写出Call的实体类
     * 创建KaifaService并连接网络数据
     * 然后通过Retrofit获取接口服务对象
     * 接口对象调用其方法获取call对象
     * 实现异步响应正文
     * call执行请求
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取Retrofit对象
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("https://www.wanandroid.com/article/list/0/")
                .addConverterFactory(GsonConverterFactory.create())//添加工厂，转化
                .build();
        //通过Retrofit获取接口服务对象
        KaifaService kaifaService = retrofit.create(KaifaService.class);
        //接口对象调用其方法获取call对象
        Call<Kaifa> all = kaifaService.getAll();//实现异步
        //all执行请求
        all.enqueue(new Callback<Kaifa>() {
            @Override
            public void onResponse(Call<Kaifa> call, Response<Kaifa> response) {
                Kaifa body=response.body();//响应正文
                Log.d(TAG, "onResponse: "+body.getData().getDatas().get(0).getApkLink());

            }

            @Override
            public void onFailure(Call<Kaifa> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());//添加错误因应

            }
        });

        //Gson gson=new Gson();
        //language=JSON
//        String s="{\n" +
//                "  \"stu_id\": 1,\n" +
//                "  \"stmu_name\":\"张三\",\n" +
//                "  \"stu_age\": 20\n" +
//                "}";
//        //language=JSON
//        String s1="{\n" +
//                "  \"code\": 200,\n" +
//                "  \"rwsuit\": [{\n" +
//                "    \"w_id\": 1,\n" +
//                "    \"w_time\": \"2020_10_1\"\n" +
//                "  }]\n" +
//                "}";
//        String xh="{\"error_code\":0,\"reason\":\"success\",\"result\":{\"data\":{\"conclusion\":\"[大吉+官运+财运+才艺]如龙得云，青云直上，智谋奋进，才略奏功\",\"analysis\":\"欲望难足希望高，计谋成功财力豪，猜疑嫉妒性自改，如龙乘云势运开。智能超人贯彻大志，富贵无比，不甘寂寞，叱吒风云之大吉数，但容易发生牢骚及贪心、欲望太多而永不知足，为其缺点。切忌沉迷投机，可免贻误前程。\"}}}";
//        Xiao  xiao= gson.fromJson(xh, Xiao.class);
//        Log.d(TAG, "onCreate: "+xiao.getResult().getData().getConclusion());

    }
}
